package com.example.basic.build_id;

public class ClientIdBuild {

    public static long buildClientID(String packageVersion) {
        String[] currentVersion = packageVersion.split("\\.");
        long clientId = 0L;
        long version_part_1 =  Integer.parseInt(currentVersion[0])*pow(2,48);
        long version_part_2 =  Integer.parseInt(currentVersion[1])*pow(2,32);
        long version_part_3 =  Integer.parseInt(currentVersion[2])*pow(2,16);
        long version_part_4 =  Integer.parseInt(currentVersion[3]);

        clientId = version_part_1 + version_part_2 + version_part_3 + version_part_4;
        return clientId;
    }

    private static long pow(int target,int power) {
        long result = 1L;
        for (int i = 0; i < power; i++) {
            result *= target;
        }
        return result;
    }

    public static String translateClientId(long clientId) {
        String version_part_1 = (clientId / pow(2,48)) + "";
        clientId = clientId % pow(2,48);
        String version_part_2 = (clientId / pow(2,32)) + "";
        clientId = clientId % pow(2,32);
        String version_part_3 = (clientId / pow(2,16)) + "";
        clientId = clientId % pow(2,16);
        String version_part_4 = clientId + "";
        return version_part_1 + "." + version_part_2 + "." + version_part_3 + "." + version_part_4;

    }
}
